package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.system.domain.FasIpqc;
import com.fas.system.service.IFasIpqcService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 巡检员Controller
 * 
 * @author ruoyi
 * @date 2022-04-27
 */
@Api(tags = "巡检员")
@RestController
@RequestMapping("/system/ipqc")
public class FasIpqcController extends BaseController
{
    @Autowired
    private IFasIpqcService fasIpqcService;

    @Resource
    private ISysDeptService deptService;


//    /**
//     * 总公司任意条件查询巡检员列表
//     * @param condition 查询条件，为空则为全查
//     * @param deptId 公司id
//     * @return
//     */
    @ApiOperation(value = "获取总公司巡检员列表")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list')")
    @GetMapping("/list1")
    public TableDataInfo list(@RequestParam(value = "condition",required = false)String condition) {

        FasIpqc fasIpqc = new FasIpqc();
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        fasIpqc.setCondition(condition);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(fasIpqc.getDeptId() == null){
                fasIpqc.setDeptId(deptId);
            }
        }
        startPage();
        List<FasIpqc> list = fasIpqcService.selectFasIpqcList(fasIpqc);
        return getDataTable(list);

        }

    @ApiOperation(value = "维保单位获取巡检员列表")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list2')")
    @GetMapping("/list2")
    public TableDataInfo list2(@RequestParam(value = "condition",required = false)String condition) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        FasIpqc fasIpqc = new FasIpqc();
        fasIpqc.setDeptId(loginUser.getDeptId());
        fasIpqc.setCondition(condition);
        System.out.println(".........部门id为："+fasIpqc.getDeptId());
        startPage();
        List<FasIpqc> list = fasIpqcService.selectFasIpqcList1(fasIpqc);
        System.out.println("..........搜索到的结果为："+list);
        return getDataTable(list);

    }

    @ApiOperation(value = "小程序获取维保员巡检员列表")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list3')")
    @GetMapping("/list3")
    public TableDataInfo list3(@RequestParam(value = "iName",required = false)String iName) {// 之前接口是将worknumber作文搜索条件
        LoginUser loginUser = SecurityUtils.getLoginUser();
        FasIpqc fasIpqc = new FasIpqc();
        fasIpqc.setiName(iName);
//        fasIpqc.setWorknumber(worknumber);
        fasIpqc.setDeptId(loginUser.getDeptId());

        System.out.println("........输入的内容为："+fasIpqc+".............."+loginUser.getDeptId());
        startPage();
        List<FasIpqc> list = fasIpqcService.selectFasIpqcList(fasIpqc);
        System.out.println(".......查询到的内容为："+list);
        return getDataTable(list);

    }


    @ApiOperation(value = "PC获取维保员巡检员列表")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list3')")
    @GetMapping("/list5")
    public TableDataInfo list5(@RequestParam(value = "condition",required = false)String condition) {
        Long deptId = SecurityUtils.getDeptId();
        SysDept sysDept = deptService.selectDeptById(deptId);
        FasIpqc fasIpqc = new FasIpqc();
        fasIpqc.setCondition(condition);
        String deptType = sysDept.getDeptType();
        if (deptType.equals("1") || deptType.equals("2")){
            if(fasIpqc.getDeptId() == null){
                fasIpqc.setDeptId(deptId);
            }
        }
        startPage();
        return getDataTable(fasIpqcService.selectFasIpqcByDeptId(fasIpqc));

    }



    @ApiOperation(value = "获取维保单位巡检员详细信息")
    @ApiImplicitParam(name = "worknumber" ,value = "工号")
    //    @PreAuthorize("@ss.hasPermi('system:ipqc:list4')")
    @GetMapping("listXiangxi")
    public TableDataInfo listXiangxi(@RequestParam(value = "worknumber",required = true)String worknumber) {
        List<FasIpqc> fasIpqcs = new ArrayList<>();
        Long deptId = SecurityUtils.getDeptId();
        FasIpqc fasIpqc = fasIpqcService.selectFasIpqcxiangxi(worknumber,deptId);
        fasIpqcs.add(fasIpqc);
        return getDataTable(fasIpqcs);
    }


//    @ApiOperation(value = "小程序获取维保单位巡检员详细信息")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list4')")
    @GetMapping("/list4")
    public TableDataInfo list4(@RequestParam(value = "worknumber",required = false)String worknumber,@RequestParam(value = "deptId",required = false)Long deptId) {

        FasIpqc fasIpqc = new FasIpqc();
//        fasIpqc.setCondition(condition);
        fasIpqc.setWorknumber(worknumber);
        fasIpqc.setDeptId(deptId);
        System.out.println("........输入的内容为："+worknumber+"........."+deptId);
        List<FasIpqc> list = fasIpqcService.selectFasIpqcList(fasIpqc);
        System.out.println(".......查询到的内容为："+list);
        return getDataTable(list);

    }



    /**
     * 查询巡检员列表
     */
//    @ApiOperation(value = "查询巡检员列表")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(FasIpqc fasIpqc)
//    {
//        startPage();
//        List<FasIpqc> list = fasIpqcService.selectFasIpqcList(fasIpqc);
//        return getDataTable(list);
//    }
    /**
     * 根据姓名、工号和联系电话任一条件查询巡检员
     */
//    @ApiOperation(value = "任意条件查询巡检员列表")
    @PreAuthorize("@ss.hasPermi('system:ipqc:listbyauto')")
    @GetMapping("/listbyauto")
    public TableDataInfo listbyauto(FasIpqc fasIpqc)
    {
        startPage();
        List<FasIpqc> list = fasIpqcService.selectFasIpqcListByAuto(fasIpqc);
        return getDataTable(list);
    }

    /**
     * 导出巡检员列表
     */
//    @ApiOperation(value = "导出巡检员")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:export')")
//    @Log(title = "巡检员", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, FasIpqc fasIpqc)
//    {
//        List<FasIpqc> list = fasIpqcService.selectFasIpqcList(fasIpqc);
//        ExcelUtil<FasIpqc> util = new ExcelUtil<FasIpqc>(FasIpqc.class);
//        util.exportExcel(response, list, "巡检员数据");
//    }

    /**
     * 获取巡检员详细信息
     */
//    @ApiOperation(value = "获取巡检员详细信息")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fasIpqcService.selectFasIpqcById(id));
    }

    /**
     * 维保单位新增巡检员
     */
    @ApiOperation(value = "新增巡检员")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:add')")
    @Log(title = "巡检员", businessType = BusinessType.INSERT)
    @PostMapping("/addIpqc")
    public AjaxResult add(@RequestBody FasIpqc fasIpqc)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fasIpqc.setDeptId(loginUser.getDeptId());
        System.out.println("巡检员所属部门id"+fasIpqc.getDeptId());
        //        fasIpqc.setWorknumber(orderldUtil.getIpqcNo());//自动增加工号
        return fasIpqcService.insertFasIpqc(fasIpqc);
    }

    /**
     * 修改巡检员
     */
//    @ApiOperation(value = "修改巡检员")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:edit')")
    @Log(title = "巡检员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FasIpqc fasIpqc)
    {
        return toAjax(fasIpqcService.updateFasIpqc(fasIpqc));
    }

    /**
     * 删除巡检员
     */
//    @ApiOperation(value = "删除巡检员")
//    @PreAuthorize("@ss.hasPermi('system:ipqc:remove')")
    @Log(title = "巡检员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fasIpqcService.deleteFasIpqcByIds(ids));
    }
}
